/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.board;

import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.JFileChooser;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mopore.jat.plugin.AbstractPlugin;
import org.mopore.jat.plugin.Email;
import org.mopore.jat.plugin.IPluginContext;
import org.mopore.jat.plugin.IJatPlugin;
import org.mopore.jat.plugin.PluginException;
import org.mopore.jat.util.ImageService;
import org.mopore.jat.util.MetaInformationService;
import org.mopore.jat.util.Version;

public class BoardFeaturePlugin extends AbstractPlugin {

    private final Log log = LogFactory.getLog(getClass().getName());

    @Override
    protected Email getAuthorsEmailImpl() throws Exception {
        return MetaInformationService.readProjectHeadEmail();
    }

    @Override
    protected String getAuthorsNameImpl() {
        return MetaInformationService.readProjectHeadName();
    }

    @Override
    public String getDisplayNameImpl() {
        return "Board Feature";
    }

    @Override
    public BufferedImage getPluginIcon() {
        BufferedImage image = ImageService.load("/images/popupicons/board.gif");
        return image;
    }

    @Override
    protected String getPluginNameImpl() {
        return BoardFeaturePlugin.class.getName();
    }

    @Override
    public MenuItem[] getMenuFocusActions() {

        return new MenuItem[] {

        new MenuItem("view clipboard") {

            /**
             * Comment for <code>serialVersionUID</code>
             */
            private static final long serialVersionUID = 1L;
            // something
        },

        new MenuItem("save as file...") {
            /**
             * Comment for <code>serialVersionUID</code>
             */
            private static final long serialVersionUID = 1L;

            {
                addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        boolean present = SaveClipBoardImageToFile
                                .checkForClipboardImageWithWarning();

                        if (present) {
                            JFileChooser chooser = new JFileChooser();
                            int returnVal = chooser.showSaveDialog(null);
                            if (returnVal == JFileChooser.APPROVE_OPTION) {
                                File selectedFile = chooser.getSelectedFile();
                                SaveClipBoardImageToFile.save(selectedFile);
                            }
                        }
                    }
                });
            }
        } };
    }

    @Override
    public MenuItem[] getMenuActions(MenuItem base) {
        return null;
    }

    @Override
    protected Version getVersionImpl() {
        // TODO Auto-generated method stub
        return MetaInformationService.readVersion();
    }

    @Override
    public boolean isFocusable() {
        return true;
    }

    @Override
    public void onPluginLoadImpl(IPluginContext pluginContext) throws PluginException {
        log.info("onPluginLoadImpl");
    }

    @Override
    public void onPluginShutDownImpl() throws PluginException {
        log.info("onPluginShutDownImpl");
    }

    @Override
    public void runPlugin(IPluginContext pluginContext) {
        log.info("now running...");
    }

    @Override
    public void doubleClicked(IJatPlugin plugin) throws PluginException {
        try {
            Runnable r = new BoardFeature();
            Thread t = new Thread(r);
            t.start();
        } catch (Exception e) {
            throw new PluginException(e);
        }
    }
}
